package com.xnyzc.lhy.mis.service.impl.punishment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;

import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.punishment.OaDUserPunishmentThawunlockParam;
import com.xnyzc.lhy.mis.entity.param.punishment.OaDUserSinglePunishmentThawunlockParam;
import com.xnyzc.lhy.mis.entity.punishment.OaDUserPunishment;
import com.xnyzc.lhy.mis.entity.punishment.OaDUserPunishmentThawunlock;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.mapper.driver.wallet.OaDWalletMapper;
import com.xnyzc.lhy.mis.mapper.punishment.OaDUserPunishmentMapper;
import com.xnyzc.lhy.mis.mapper.punishment.OaDUserPunishmentThawunlockMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.service.punishment.IOaDUserPunishmentThawunlockService;
import com.xnyzc.lhy.mis.entity.driver.wallet.OaDWallet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 司机处罚解封解冻表 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-10-27
 */
@Service
public class OaDUserPunishmentThawunlockServiceImpl extends ServiceImpl<OaDUserPunishmentThawunlockMapper, OaDUserPunishmentThawunlock> implements IOaDUserPunishmentThawunlockService {
    @Autowired
    OaDUserMapper oaDUserMapper;
    @Autowired
    OaDUserPunishmentMapper oaDUserPunishmentMapper;
    @Autowired
    OaDWalletMapper oaDWalletMapper;
    @Autowired
    OaDUserPunishmentThawunlockMapper oaDUserPunishmentThawunlockMapper;
    @Override
    @Transactional
    public Rv globalUnsealingAndThawingOperation(OaDUserPunishmentThawunlockParam oaDUserPunishmentThawunlockParam) {
        /***
         * 添加司机处罚解冻表   oa_d_user_punishment_thawunlock
         *
         * */
        OaDUserPunishmentThawunlock oaDUserPunishmentThawunlock = new OaDUserPunishmentThawunlock();
        oaDUserPunishmentThawunlock.setThawunlockId(IDUtil.nextId());
        oaDUserPunishmentThawunlock.setDriverId(oaDUserPunishmentThawunlockParam.getDriverId());
        oaDUserPunishmentThawunlock.setCreateTime(new Date());
        oaDUserPunishmentThawunlock.setNote(oaDUserPunishmentThawunlockParam.getNote());
        oaDUserPunishmentThawunlock.setThawunlockType(oaDUserPunishmentThawunlockParam.getType());
        oaDUserPunishmentThawunlock.setThawunlockWhy(oaDUserPunishmentThawunlockParam.getReasult());
        Long sysUserId = TokenUtil.getTokenUserId();
        oaDUserPunishmentThawunlock.setSysUserId(sysUserId);

        if(oaDUserPunishmentThawunlockParam.getType().equals(1)){
            /**
             * 1.封号期限操作   表 oa_d_user  中的
             * thawing_time`  '解冻时间', （暂时没有自己做添加）
             * `account_status`  '账号状态(正常-0 待提交-1 待审核-2 审核驳回-3 未激活-4 封号中-5)',
             * oa_d_user_punishment  表 中  punishment_titles_deadline_finished  账号封禁是否执行完（0 未执行完 1 已执行完）定时任务操作
             * */
            //根据司机id 查询oa_d_user 表
            QueryWrapper<OaDUser> queryWrapper = new QueryWrapper();
            queryWrapper.eq(OaDUser.DRIVER_ID,oaDUserPunishmentThawunlockParam.getDriverId());
            queryWrapper.eq(OaDUser.ACCOUNT_STATUS,5);
            OaDUser oaDUser = oaDUserMapper.selectOne(queryWrapper);
            oaDUser.setAccountStatus(0);
            oaDUser.setThawingTime(new Date());
            oaDUserMapper.updateById(oaDUser);
            //查询 oa_d_user_punishment 表
            QueryWrapper<OaDUserPunishment> oaDUserPunishmentQueryWrapper = new QueryWrapper();
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.DRIVER_ID,oaDUserPunishmentThawunlockParam.getDriverId());
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_TITLES_DEADLINE_FINISHED,0);
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_STATUS,1);
            List<OaDUserPunishment> oaDUserPunishments =  oaDUserPunishmentMapper.selectList(oaDUserPunishmentQueryWrapper);
            if(oaDUserPunishments.size()>0){
                for (OaDUserPunishment oaDUserPunishment : oaDUserPunishments) {
                    oaDUserPunishment.setPunishmentTitlesDeadlineFinished(1);
                    oaDUserPunishment.setThawunlockId(oaDUserPunishmentThawunlock.getThawunlockId());
                    oaDUserPunishmentMapper.updateById(oaDUserPunishment);
                }
            }
        }else{

            /**
             * 2.冻结期限操作   表 oa_d_wallet 中的
             * `thawing_time` '解冻时间',
             * `logon_status` '钱包状态(0:可用,1:不可用)',
             *  oa_d_user_punishment  表 中  punishment_account_frozen_finished    账户冻结是否执行完（0 未执行完 1 已执行完）定时任务操作
             *  次数
             * */
            QueryWrapper<OaDWallet> queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq(OaDWallet.DRIVER_ID,oaDUserPunishmentThawunlockParam.getDriverId());
            queryWrapper1.eq(OaDWallet.LOGON_STATUS,1);
            OaDWallet oaDWallet = oaDWalletMapper.selectOne(queryWrapper1);
            if(oaDWallet != null){
                oaDWallet.setLogonStatus(0);
                oaDWallet.setThawingTime(new Date());
                oaDWalletMapper.updateById(oaDWallet);
                //查询 oa_d_user_punishment 表
                QueryWrapper<OaDUserPunishment> oaDUserPunishmentQueryWrapper = new QueryWrapper();
                oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.DRIVER_ID,oaDUserPunishmentThawunlockParam.getDriverId());
                oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_ACCOUNT_FROZEN_FINISHED,0);
                oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_STATUS,2);
                List<OaDUserPunishment> oaDUserPunishments = oaDUserPunishmentMapper.selectList(oaDUserPunishmentQueryWrapper);
                if(oaDUserPunishments.size()>0){
                    for (OaDUserPunishment oaDUserPunishment : oaDUserPunishments) {
                        oaDUserPunishment.setPunishmentAccountFrozenFinished(1);
                        oaDUserPunishment.setThawunlockId(oaDUserPunishmentThawunlock.getThawunlockId());
                        oaDUserPunishmentMapper.updateById(oaDUserPunishment);
                    }
                }
            }else{
                return Rv.wrap(EErrorCode.noData,"此司机不存在封冻操作");
            }
        }
        oaDUserPunishmentThawunlockMapper.insert(oaDUserPunishmentThawunlock);
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 单条处罚解封解冻
     * @param oaDUserSinglePunishmentThawunlockParam
     * @return
     */
    @Override
    @Transactional
    public Rv partOfDriverUnsealDefrostingOperation(OaDUserSinglePunishmentThawunlockParam oaDUserSinglePunishmentThawunlockParam) {
        //创建解封解冻记录
        OaDUserPunishmentThawunlock oaDUserPunishmentThawunlock = new OaDUserPunishmentThawunlock();
        oaDUserPunishmentThawunlock.setThawunlockId(IDUtil.nextId());
        oaDUserPunishmentThawunlock.setDriverId(oaDUserSinglePunishmentThawunlockParam.getDriverId());
        oaDUserPunishmentThawunlock.setCreateTime(new Date());

        //解封解冻类型 1 解封 2 解冻
        oaDUserPunishmentThawunlock.setThawunlockType(oaDUserSinglePunishmentThawunlockParam.getType());
        //提前解封解冻原因
        oaDUserPunishmentThawunlock.setThawunlockWhy(oaDUserSinglePunishmentThawunlockParam.getReasult());
        //备注
        oaDUserPunishmentThawunlock.setNote(oaDUserSinglePunishmentThawunlockParam.getNote());

        Long sysUserId = TokenUtil.getTokenUserId();
        oaDUserPunishmentThawunlock.setSysUserId(sysUserId);

        if(oaDUserSinglePunishmentThawunlockParam.getType().equals(1)){
            //解封
            //查询处罚记录
            QueryWrapper<OaDUserPunishment> oaDUserPunishmentQueryWrapper = new QueryWrapper();
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.DRIVER_ID,oaDUserSinglePunishmentThawunlockParam.getDriverId());
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_ID,oaDUserSinglePunishmentThawunlockParam.getPunishmentId());
            OaDUserPunishment oaDUserPunishment =  oaDUserPunishmentMapper.selectOne(oaDUserPunishmentQueryWrapper);

            if(oaDUserPunishment != null){
                //更新处罚记录状态
                oaDUserPunishment.setPunishmentTitlesDeadlineFinished(1);
                oaDUserPunishment.setThawunlockId(oaDUserPunishmentThawunlock.getThawunlockId());
                oaDUserPunishmentMapper.updateById(oaDUserPunishment);

                //更新司机状态
                QueryWrapper<OaDUser> queryWrapper = new QueryWrapper();
                queryWrapper.eq(OaDUser.DRIVER_ID, oaDUserSinglePunishmentThawunlockParam.getDriverId());
                //封号中-5
                queryWrapper.eq(OaDUser.ACCOUNT_STATUS,5);
                OaDUser oaDUser = oaDUserMapper.selectOne(queryWrapper);
                if(oaDUser != null){
                    //更改账号封号时间
                    Integer punishmentTitlesDeadlineFinished = unAbs(oaDUserPunishment.getPunishmentTitlesDeadline());
                    Date date = DateUtil.dateAddDays(oaDUser.getThawingTime(),punishmentTitlesDeadlineFinished);
                    oaDUser.setThawingTime(date);

                    //如果封号时间小于当前时间，设置账号状态=0正常
                    if(date.compareTo(new Date()) < 0 ){
                        oaDUser.setAccountStatus(0);
                    }
                    oaDUser.setThawingTime(new Date());
                    oaDUserMapper.updateById(oaDUser);
                }
            }else{
                return Rv.error(EErrorCode.punishmentNone);
            }
        }else{
            //解冻
            //查询处罚记录
            QueryWrapper<OaDUserPunishment> oaDUserPunishmentQueryWrapper = new QueryWrapper();
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.DRIVER_ID,oaDUserSinglePunishmentThawunlockParam.getDriverId());
            oaDUserPunishmentQueryWrapper.eq(OaDUserPunishment.PUNISHMENT_ID,oaDUserSinglePunishmentThawunlockParam.getPunishmentId());
            OaDUserPunishment oaDUserPunishment =  oaDUserPunishmentMapper.selectOne(oaDUserPunishmentQueryWrapper);

            if(oaDUserPunishment != null){
                //更新处罚记录状态
                oaDUserPunishment.setPunishmentAccountFrozenFinished(1);
                oaDUserPunishment.setThawunlockId(oaDUserPunishmentThawunlock.getThawunlockId());
                oaDUserPunishmentMapper.updateById(oaDUserPunishment);

                //查询司机钱包状态
                QueryWrapper<OaDWallet> queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq(OaDWallet.DRIVER_ID,oaDUserSinglePunishmentThawunlockParam.getDriverId());
                //钱包状态(0:可用,1:不可用)
                queryWrapper1.eq(OaDWallet.LOGON_STATUS,1);
                OaDWallet oaDWallet = oaDWalletMapper.selectOne(queryWrapper1);

                if(oaDWallet != null){
                    //修改冻结时间
                    Integer punishmentAccountFrozenFinished  =  unAbs(oaDUserPunishment.getPunishmentAccountFrozen());
                    Date date = DateUtil.dateAddDays(oaDWallet.getThawingTime(),punishmentAccountFrozenFinished);
                    oaDWallet.setThawingTime(date);

                    //如果冻结时间小于当前时间，设置钱包状态为0=可用
                    if(date.compareTo(new Date()) < 0 ){
                        oaDWallet.setLogonStatus(0);
                    }
                    oaDWalletMapper.updateById(oaDWallet);
                }
            }else{
                return Rv.error(EErrorCode.punishmentNone);
            }
        }
        oaDUserPunishmentThawunlockMapper.insert(oaDUserPunishmentThawunlock);
        return Rv.wrap(EErrorCode.success);
    }

    public static int unAbs(int a) {
        return (a > 0) ? -a : a;
    }
}
